﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
using System.Web;
using System.Drawing.Drawing2D;

namespace QD.Web.Upload
{
    public class Image
    {
        public static Bitmap CutImage(Bitmap srcImage, int startPointX, int startPointY, int maxWidth, int maxHeight)
        {
            if ((srcImage.Height <= maxHeight) && (srcImage.Width <= maxWidth))
            {
                return srcImage.Clone(new Rectangle(0, 0, srcImage.Width, srcImage.Height), srcImage.PixelFormat);
            }
            if (srcImage.Height < (maxHeight + startPointY))
            {
                maxHeight = srcImage.Height - startPointY;
            }
            if (srcImage.Width < (maxWidth + startPointX))
            {
                maxWidth = srcImage.Width - startPointX;
            }
            if ((maxHeight < 1) || (maxWidth < 1))
            {
                return null;
            }
            return srcImage.Clone(new Rectangle(startPointX, startPointY, maxWidth, maxHeight), srcImage.PixelFormat);
        }
        public static Bitmap CutImageRedraw(Bitmap srcImage, int destPointX, int destPointY, int cutWidth, int cutHeight, int srcPointX, int srctPointY, int outputWidth, int outputHeight)
        {
            if ((srcImage.Height <= cutHeight) && (srcImage.Width <= cutWidth))
            {
                return srcImage.Clone(new Rectangle(0, 0, srcImage.Width, srcImage.Height), srcImage.PixelFormat);
            }
            if (srcImage.Height < (cutHeight + srctPointY))
            {
                cutHeight = srcImage.Height - srctPointY;
            }
            if (srcImage.Width < (cutWidth + srcPointX))
            {
                cutWidth = srcImage.Width - srcPointX;
            }
            if ((cutWidth < 1) || (cutWidth < 1))
            {
                return null;
            }
            Bitmap image = new Bitmap(outputWidth, outputHeight);
            Graphics graphics = Graphics.FromImage(image);
            graphics.InterpolationMode = InterpolationMode.High;
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.Clear(Color.White);
            graphics.DrawImage(srcImage, new Rectangle(destPointX, destPointY, cutWidth, cutHeight), new Rectangle(srcPointX, srctPointY, cutWidth, cutHeight), GraphicsUnit.Pixel);
            return image;
        }
        public static ImageFormat GetFormat(string name)
        {
            switch (name.Substring(name.LastIndexOf(".") + 1).ToLower())
            {
                case "jpg":
                case "jpeg":
                    return ImageFormat.Jpeg;

                case "bmp":
                    return ImageFormat.Bmp;

                case "png":
                    return ImageFormat.Png;

                case "gif":
                    return ImageFormat.Gif;
            }
            return ImageFormat.Jpeg;
        }
        public static Size ResizeImage(int width, int height, int maxWidth, int maxHeight)
        {
            int num4;
            int num5;
            decimal num = maxWidth;
            decimal num2 = maxHeight;
            decimal num3 = num / num2;
            decimal num6 = width;
            decimal num7 = height;
            if ((num6 > num) || (num7 > num2))
            {
                decimal num8;
                if ((num6 / num7) > num3)
                {
                    num8 = num6 / num;
                    num4 = Convert.ToInt32((decimal)(num6 / num8));
                    num5 = Convert.ToInt32((decimal)(num7 / num8));
                }
                else
                {
                    num8 = num7 / num2;
                    num4 = Convert.ToInt32((decimal)(num6 / num8));
                    num5 = Convert.ToInt32((decimal)(num7 / num8));
                }
            }
            else
            {
                num4 = width;
                num5 = height;
            }
            return new Size(num4, num5);
        }
    }
}